package com.example.algorithm.interview;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 招银笔试题2
 *
 * @description 从给定字符串提取所有数字
 * @date 2023-12-16
 */
public class ExtractNum {

    public static void main(String[] args) {
        ArrayList<Integer> res = new ExtractNum().extraNum("c12m23b3n4t56");
        System.out.println(res);
    }

    public ArrayList<Integer> extraNum(String s) {
        char[] charArray = s.toCharArray();
        for (char c : charArray) {
            receiveNum(c);
        }
        ArrayList<Integer> finalRes = new ArrayList<>();
        for (StringBuilder stringBuilder : res) {
            finalRes.add(Integer.parseInt(stringBuilder.toString()));
        }
        return finalRes;
    }

    List<StringBuilder> res = new ArrayList<>();
    StringBuilder tmp = new StringBuilder();
    boolean add = false;
    Set<Character> numberSet = new HashSet<>();

    // 坑爹的在线编辑器不支持匿名内部类写法，之前在华为od笔试题就栽过
    {
        for (int i = 0; i < 10; i++) {
            // 48对应的数字是0
            numberSet.add((char) (48 + i));
        }
    }

    private void receiveNum(char s) {
        if (numberSet.contains(s)) {
            if (!add) {
                res.add(tmp);
                add = true;
            }
            tmp.append(s);
        } else {
            tmp = new StringBuilder();
            add = false;
        }
    }

}
